package com.hospital.hospitalsystem.modules.user.controller;

import com.hospital.hospitalsystem.base.BaseResponse;
import com.hospital.hospitalsystem.config.TokenVerify;
import com.hospital.hospitalsystem.modules.base.biz.ProvinceBiz;
import com.hospital.hospitalsystem.modules.user.biz.AddressBiz;
import com.hospital.hospitalsystem.modules.user.dto.AddressReq;
import com.hospital.hospitalsystem.modules.base.dto.ProvinceReq;
import com.hospital.hospitalsystem.modules.base.dto.ProvinceRsp;
import com.hospital.hospitalsystem.modules.user.dto.AddressRsp;
import com.hospital.hospitalsystem.utils.LoginUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author qiancheng@cqxyy.net
 * @date 2023/2/16
 */
@RestController
@RequestMapping("/address")
@Api(tags = "地址管理")
@Validated
public class AddressController {

    @Autowired
    private AddressBiz addressBiz;

    @Autowired
    private ProvinceBiz provinceBiz;

    @Autowired
    private LoginUtils loginUtils;

    @ApiOperation(value = "查询省份")
    @RequestMapping(path = "/list-province",method = RequestMethod.GET)
    public BaseResponse listProvince(){
        ProvinceRsp result = provinceBiz.getListProvince();
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "查询市")
    @RequestMapping(path = "/list-city",method = RequestMethod.POST)
    public BaseResponse listCity(@RequestBody ProvinceReq req){
        ProvinceRsp result = provinceBiz.getlistCity(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "查询区")
    @RequestMapping(path = "/list-area",method = RequestMethod.POST)
    public BaseResponse listArea(@RequestBody ProvinceReq req){
        ProvinceRsp result = provinceBiz.getListArea(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "查询街道")
    @RequestMapping(path = "/list-town",method = RequestMethod.POST)
    public BaseResponse listTown(@RequestBody ProvinceReq req){
        ProvinceRsp result = provinceBiz.getListTown(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "添加快递地址信息")
    @RequestMapping(path = "/add",method = RequestMethod.POST)
    @TokenVerify(type = "user")
    public BaseResponse add(@RequestBody AddressReq req){
        req.setUserUuid(loginUtils.getUser().getUserUuid());
        addressBiz.add(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "查找快递地址信息列表")
    @RequestMapping(path = "/list",method = RequestMethod.GET)
    @TokenVerify(type = "user")
    public BaseResponse list(AddressReq req){
        req.setUserUuid(loginUtils.getUser().getUserUuid());
        List<AddressRsp> result = addressBiz.list(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "根据uuid获取快递地址信息")
    @RequestMapping(path = "/get",method = RequestMethod.POST)
    @TokenVerify(type = "user,medicine")
    public BaseResponse getByUuid(@RequestBody AddressReq req){
        if (loginUtils.getUser().getRoles().equals("user")) {
            req.setUserUuid(loginUtils.getUser().getUserUuid());
        }
        AddressRsp result = addressBiz.getByUuid(req);
        return BaseResponse.success(result);
    };

    @ApiOperation(value = "根据uuid删除快递地址信息")
    @RequestMapping(path = "/delete",method = RequestMethod.POST)
    @TokenVerify(type = "user")
    public BaseResponse delete(@RequestBody AddressReq req){
        req.setUserUuid(loginUtils.getUser().getUserUuid());
        addressBiz.delete(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "根据uuid修改快递地址信息")
    @RequestMapping(path = "/update",method = RequestMethod.POST)
    @TokenVerify(type = "user")
    public BaseResponse update(@RequestBody AddressReq req){
        req.setUserUuid(loginUtils.getUser().getUserUuid());
        addressBiz.update(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "根据uuid设置默认收货地址")
    @RequestMapping(path = "/default",method = RequestMethod.POST)
    @TokenVerify(type = "user")
    public BaseResponse isDefault(@RequestBody AddressReq req){
        req.setUserUuid(loginUtils.getUser().getUserUuid());
        addressBiz.isDefault(req);
        return BaseResponse.success();
    };

    @ApiOperation(value = "查找默认收货地址")
    @RequestMapping(path = "/search-default",method = RequestMethod.GET)
    @TokenVerify(type = "user")
    public BaseResponse searchDefault(){
        AddressReq addressReq = new AddressReq();
        addressReq.setUserUuid(loginUtils.getUser().getUserUuid());
        AddressRsp addressRsp = addressBiz.searchDefault(addressReq);
        return BaseResponse.success(addressRsp);
    };
}
